/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.motherfoca.interfaces.usuario;

import com.motherfoca.DAO.CMotherFocaDAOSingleton;
import com.motherfoca.VO.CUsuario;
import javax.swing.JOptionPane;

/**
 *
 * @author langeblanc
 */
public class JFCambioContrasenna extends javax.swing.JFrame {

    /**
     * Creates new form JFCambioContrasenna
     */
    public JFCambioContrasenna() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLConfirm = new javax.swing.JLabel();
        jBCambiarContrasenna = new javax.swing.JButton();
        jLPwdAnterior = new javax.swing.JLabel();
        jLNew = new javax.swing.JLabel();
        jPFPwdAnterior = new javax.swing.JPasswordField();
        jPFNew = new javax.swing.JPasswordField();
        jPFConfirm = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLConfirm.setText("Confirmar Contraseña");

        jBCambiarContrasenna.setText("Cambiar Contraseña");
        jBCambiarContrasenna.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCambiarContrasennaActionPerformed(evt);
            }
        });

        jLPwdAnterior.setText("Contraseña Anterior");

        jLNew.setText("Nueva Contraseña");

        jPFPwdAnterior.setEditable(false);
        jPFPwdAnterior.setText("1234");

        jPFNew.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jPFNewKeyTyped(evt);
            }
        });

        jPFConfirm.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jPFConfirmKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLConfirm)
                        .addComponent(jLNew, javax.swing.GroupLayout.Alignment.LEADING))
                    .addComponent(jLPwdAnterior))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPFConfirm)
                    .addComponent(jPFNew)
                    .addComponent(jPFPwdAnterior, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE))
                .addGap(28, 28, 28))
            .addGroup(layout.createSequentialGroup()
                .addGap(114, 114, 114)
                .addComponent(jBCambiarContrasenna)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPFPwdAnterior, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLPwdAnterior))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPFNew, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLNew))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPFConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLConfirm))
                .addGap(32, 32, 32)
                .addComponent(jBCambiarContrasenna)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBCambiarContrasennaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCambiarContrasennaActionPerformed
        if (jPFNew.getText().equals(jPFConfirm.getText())) {
            CUsuario usuario = CMotherFocaDAOSingleton.getInstance().getUsuarioByUser("admin");
            usuario.setContrasenna(jPFNew.getText());
            CMotherFocaDAOSingleton.getInstance().actualizar(usuario);
            JOptionPane.showMessageDialog(this, "La contraseña ha sido actualizada con éxito ");
            JFLogin login = new JFLogin();
            this.dispose();
            login.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(this, "Las contraseñas no coinciden");
        }
    }//GEN-LAST:event_jBCambiarContrasennaActionPerformed
    
    private void jPFNewKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPFNewKeyTyped
        if (jPFNew.getText().length() == 12) {
            evt.consume();
        }
    }//GEN-LAST:event_jPFNewKeyTyped
    
    private void jPFConfirmKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPFConfirmKeyTyped
        if (jPFConfirm.getText().length() == 12) {
            evt.consume();
        }
    }//GEN-LAST:event_jPFConfirmKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFCambioContrasenna.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFCambioContrasenna.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFCambioContrasenna.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFCambioContrasenna.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFCambioContrasenna().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBCambiarContrasenna;
    private javax.swing.JLabel jLConfirm;
    private javax.swing.JLabel jLNew;
    private javax.swing.JLabel jLPwdAnterior;
    private javax.swing.JPasswordField jPFConfirm;
    private javax.swing.JPasswordField jPFNew;
    private javax.swing.JPasswordField jPFPwdAnterior;
    // End of variables declaration//GEN-END:variables
}
